/**
 * Created by wry on 2015/3/24.
 */
'use strict';

angular.module('backofficeApp.backendConsole.controllers', []).
    controller('backendConsoleCtrl', ['$scope', '$location', '$http', function ($scope, $location, $http) {

        var startTime;
        var endTime;

        $('.start_time').datetimepicker({
            language: 'zh-CN',
            weekStart: 1,
            todayBtn: 1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            forceParse: 0,
            showMeridian: 1
        }).on('changeDate', function (ev) {
            var date = ev.date;
            startTime = new Date(date.setHours(date.getHours() - 8));
        });
        $('.end_time').datetimepicker({
            language: 'zh-CN',
            weekStart: 1,
            todayBtn: 1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            forceParse: 0,
            showMeridian: 1
        }).on('changeDate', function (ev) {
            var date = ev.date;
            endTime = new Date(date.setHours(date.getHours() - 8));
        });


        $scope.getQuestionStatistics = function () {
            if (!startTime || !endTime) {
                alert('请选择时间段')
                return;
            }

            var opt = {
                startTime: startTime,
                endTime: endTime
            };

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/marketing/getCommunityQuestionStatistics', opt, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (result) {
                $scope.statistics = result.data;
            }).error(function () {
            });
        }
    }]).controller('downloadCtrl', ['$scope', '$location', '$http', 'CommunityService', function ($scope, $location, $http, communityService) {

        $scope.$on('$viewContentLoaded', function () {
            $scope.communityLabelList = communityService.getAllCommunityService();
        });

        $('#startDate').datepicker().on('changeDate', function (ev) {
            $scope.startTime = ev.date;
        });

        $('#endDate').datepicker().on('changeDate', function (ev) {
            $scope.endTime = ev.date;
        });

        $scope.createUserCsv = function () {

            if (!$scope.startTime || !$scope.endTime) {
                alert('请选择时间段');
                return;
            }
            var opt = {
                startTime: $scope.startTime,
                endTime: $scope.endTime
            };
            if ($scope.communityId != null) {
                opt.community = $scope.communityId;
            }

            console.log($scope.communityId)
            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/marketing/createUserCsv', opt, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (result) {
                alert('创建完成')
            }).error(function () {
            });
        };

        $scope.createUgcCsv = function () {

            if (!$scope.startTime || !$scope.endTime) {
                alert('请选择时间段');
            }
            var opt = {
                startTime: $scope.startTime,
                endTime: $scope.endTime
            };

            if ($scope.communityId != null) {
                opt.community = $scope.communityId;
            }

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/marketing/createUgcCsv', opt, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (result) {
                alert('创建完成')
            }).error(function () {
            });

        };
    }]).controller('pushRecordCtrl', ['$scope', '$location', '$http', function ($scope, $location, $http) {

        $scope.getPushRecord = function () {
            $http.get('http://' + backOfficeHost + '/rest/v1.0/backoffice/marketing/getOperatePushStatistics', {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (result) {
                $scope.pushRecord = result.data;
            }).error(function () {
            });
        };

        $scope.getPushRecord();
    }]);